#include "draggedrelation.h"

#include "uml/umlitem.h"
#include "uml/umlrelationalitem.h"
#include "uml/relations/umlrelation.h"



DraggedRelation::DraggedRelation(QGraphicsItemGroup *parent) : QGraphicsItemGroup(parent)
{
    this->setFlags(QGraphicsItem::ItemIsMovable | QGraphicsItem::ItemIsSelectable);

    // init pen
    this->_pen.setColor(Qt::black);
    this->_pen.setWidth(1);

    // init graphics item
    this->_lineGraphicsItem.setPen(this->_pen);
    this->addToGroup(&this->_lineGraphicsItem);
}

UmlRelationalItem* DraggedRelation::startItem()
{
    return this->_startItem;
}

void DraggedRelation::setStartItem(UmlRelationalItem *item)
{
    this->_startItem = item;

    this->_startPos = this->mapToScene(item->pos());
}

void DraggedRelation::setMousePos(QPointF pos)
{
    QLineF line;

    line.setP1(this->_startPos);
    line.setP2(pos);

    this->_lineGraphicsItem.setLine(line);

    QMatrix m;
    m.rotate(-line.angle());

    this->_decoratorGraphicsItem.setPos(pos);
    this->_decoratorGraphicsItem.setMatrix(m.rotate(180));
}

void DraggedRelation::setDecorator(QPolygonF decorator)
{
    this->_decoratorGraphicsItem.setPolygon(decorator);
    this->_decoratorGraphicsItem.setPen(this->_pen);
    this->addToGroup(&this->_decoratorGraphicsItem);
}

UmlRelation* DraggedRelation::relation()
{
    return this->_relation;
}

void DraggedRelation::setRelation(UmlRelation *relation)
{
    this->_relation = relation;
}
